﻿using AutoMapper;
using MediatR;
using Production_Set.API.Application.Commands.Resourctes;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using System.Security.Cryptography;
using THE_WIND_RISES.Common.Domans.Produce.Customers;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Customerser
{
    /// <summary>
    /// 客户添加
    /// </summary>
    public class CustomerCreateHandler : IRequestHandler<CustomerCreateCommend, Result>
    {
        private readonly IBaseRepository<Customer> Customer;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="Resource"></param>
        public CustomerCreateHandler(IMapper mapper, IBaseRepository<Customer> Customer)
        {
            this.mapper = mapper;
            this.Customer = Customer;
        }

        public async Task<Result> Handle(CustomerCreateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Added };

            var AddData = mapper.Map<Customer>(request);
            string sql = "insert into Customer values(@CustomerId,@CustomerNo,@CustomerName,@CustomerType,@CustomerPhone,@CustomerEmail,@IndustryId,@CustomerAddress," +
                "@CountryId,@CityId,@CustomerLabel,@CustomerLogotype,@CustomerState,@ApprovalStatus,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = Customer.Add(sql, new
            {
                CustomerId = AddData.CustomerId,
                CustomerNo = AddData.CustomerNo,
                CustomerName = AddData.CustomerName,
                CustomerType = AddData.CustomerType,
                CustomerPhone = AddData.CustomerPhone,
                CustomerEmail = AddData.CustomerEmail,
                IndustryId = AddData.IndustryId,
                CustomerAddress = request.CustomerAddress,
                CountryId = request.CountryId,
                CityId = request.CityId,
                CustomerLabel = request.CustomerLabel,
                CustomerLogotype = AddData.CustomerLogotype,
                CustomerState = AddData.CustomerState,
                ApprovalStatus = AddData.ApprovalStatus,
                IsDelete = AddData.IsDelete,
                Handle= AddData.Handle = "李四",
                HandleContext = AddData.HandleContext = "添加",
                HandleTime = AddData.HandleTime = DateTime.Now,
                Pid = AddData.Pid,
            });
            if (AddNum > 0)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "添加成功";
                return results;
            }
            else
            {
                return results;
            }
        }
    }
}
